package com.dlust.notestxx;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dlust.notestxx.Model.Note;

import java.text.SimpleDateFormat;
import java.util.Date;

public class NotesTakeActivity extends AppCompatActivity {
    EditText titleEd, noteEd;
    ImageView saveBtn;
    Note note;
    Boolean isOldNote = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes_take);

        saveBtn = findViewById(R.id.saveBtn);
        titleEd = findViewById(R.id.titleEdit);
        noteEd = findViewById(R.id.noteEdit);

        note = new Note();

        try {
            note = (Note) getIntent().getSerializableExtra("old_note");
            titleEd.setText(note.getTitle());
            noteEd.setText(note.getNotes());
            isOldNote = true;
        }catch (Exception e) {
            e.printStackTrace();
        }

        saveBtn.setOnClickListener(v -> {
            String title = titleEd.getText().toString();
            String description = noteEd.getText().toString();
            if(description.isEmpty()) {
                Toast.makeText(NotesTakeActivity.this, "请输入具体内容", Toast.LENGTH_SHORT).show();
            }
            @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
            Date date = new Date();
            if(!isOldNote) note = new Note();
            note.setTitle(title);
            note.setNotes(description);
            note.setDate(format.format(date));
            Intent intent = new Intent();
            intent.putExtra("note", note);
            setResult(Activity.RESULT_OK, intent);
            finish();
        });
    }
}